import http from "@/utils/http";
import type {
    productStorageParamsType,
    productStorageAddParamsType,
    productStorageUpdateParamsType,
    PaginationType,
    ProductStorageSearchType
} from "./params";
import { headerCommon, headerMaster } from "../header";
import { productStorageResponseData, productStorageAddResponseData } from "./res";


// 标准写法
enum API {
    GET_productSTORAGE_LIST_API = "/1.1/classes/productStorage",
    POST_productSTORAGE_ADD_API = "/1.1/classes/productStorage",
    DELETE_productSTORAGE_API = "/1.1/classes/productStorage",
    PUT_productSTORAGE_UPDATE_API = "/1.1/classes/productStorage",
}

// 获取产品入库列表
export const getproductStorageListApi = (data?: ProductStorageSearchType, params?: PaginationType) =>
    http.get<any, productStorageResponseData>(
        API.GET_productSTORAGE_LIST_API +
        `${data ? `?where=${JSON.stringify(data)}` : ""}`,
        {
            headers: headerCommon,
            ...(params ? { params: { limit: params.pageSize, skip: (params.currentPage - 1) * params.pageSize } } : {})
        }
    );


// 删除产品入库
export const deleteproductStorageApi = (objectId: string) =>
    http.delete<any, { error?: string }>(
        API.DELETE_productSTORAGE_API + `/${objectId}`,
        {
            headers: headerMaster,
        }
    );

// 新增产品入库
export const postproductStorageAddApi = (data: productStorageAddParamsType) =>
    http.post<any, productStorageAddResponseData>(
        API.POST_productSTORAGE_ADD_API,
        {
            ...data,
            status: "未入库",
        },
        {
            headers: headerCommon,
        }
    );

export const putproductStorageUpdateApi = (data: productStorageUpdateParamsType) =>
    http.put<any, productStorageAddResponseData>(
        API.PUT_productSTORAGE_UPDATE_API + `/${data.objectId}`,
        data,
        {
            headers: headerMaster,
        }
    );

/**
 * 执行入库操作
 * @param objectId 领料单唯一标识
 */
export const executeInStorageApi = (objectId: string) => {
    return http.put<any, { updatedAt: string }>(`${API.PUT_productSTORAGE_UPDATE_API}/${objectId}`,
        {
            status: "已入库", // 更新为已入库状态
        },
        {
            headers: headerMaster,
        });
};

/**
 * 执行未入库操作
 * @param objectId 领料单唯一标识
 */
export const executeOutStorageApi = (objectId: string) => {
    return http.put<any, { updatedAt: string }>(`${API.PUT_productSTORAGE_UPDATE_API}/${objectId}`,
        {
            status: "未入库", // 撤销已入库状态
        },
        {
            headers: headerMaster,
        });
};
